import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/emergency_event/emergency_event_model.dart';
import 'package:ls_work/emergency_event/response/emergency_event_list_data.dart';
import 'package:ls_work/emergency_event/response/emergency_event_type_data.dart';

import '../ui/edit_emergency_event_page.dart';
import '../ui/emergency_event_detail_page.dart';

class EmergencyEventRecordViewModel
    extends BaseListViewModel<EmergencyEventListData> {
  EmergencyEventRecordViewModel(super.context);

  var model = EmergencyEventModel();

  List<EmergencyEventTypeData> typeList = [];

  EmergencyEventTypeData? eventNature;

  EmergencyEventTypeData? eventType;

  @override
  Future<List<EmergencyEventListData>> requestListData() async {
    if (typeList.isEmpty) {
      typeList = await model.emergencyEventType();
    }
    return await model.emergencyEventRecord(
        eventType: eventType?.codeValue,
        eventNature: eventNature?.codeValue,
        page: page,
        rows: pageLength,
        stratDateLike: startDateStr.isNullOrEmpty()?null :'${startDateStr} 00:00:00');
  }

  void chooseEventNature() {
    DataPickerView(context, typeList, currentFirstData: eventNature,
        onSubmitListener: (p1, p2, p3) {
      eventNature = typeList[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void chooseEventType() {
    if (eventNature == null) {
      showToast("请先选择事件定性");
      return;
    }
    var list = eventNature!.codeValueList ?? [];
    DataPickerView(context, list, currentFirstData: eventType,
        onSubmitListener: (p1, p2, p3) {
      eventType = list[p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void resetClick() {
    eventNature = null;
    eventType = null;
    startDateStr = "";
    startDateTime = null;
    notifyListeners();
    requestRefresh();
  }

  @override
  void onCreated() {
    super.onCreated();
    requestRefresh();
  }

  Future<void> toDetail(EmergencyEventListData item) async {
    await Go().push(EmergencyEventDetailPage(id: item.id,));
    requestRefresh();
  }

  Future<void> addRecord() async {
    var data = await Go().push(EditEmergencyEventPage());
    requestRefresh();
  }

}
